package com.commerce.constellation;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.mob.MobSDK;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private Button qqLogin;
    private CheckBox checkbox;
    private String userName = null;

    private final PlatformActionListener platformActionListener =new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            userName = qq.getDb().getUserName();
            Intent intent = new Intent(LoginActivity.this, TopActivity.class);
            intent.putExtra("userName",userName);
            startActivity(intent);
            finish();
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {

        }

        @Override
        public void onCancel(Platform platform, int i) {

        }
    };
    private Platform qq;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        int n = grantResults.length;
        if (n < 1)
            return ;
        for (int i = 0; i < n; i++){
            if (PackageManager.PERMISSION_GRANTED != grantResults[i]){ //用户未授予权限定义中对应的权限
                //未授权，不能执行需要权限的操作。
                finish();
            }else{
                //已授权可以执行相应操作
            }
        }
    }
    private void initViews() {
        qqLogin = findViewById(R.id.qqLogin);
        qqLogin.setEnabled(false);
        checkbox = findViewById(R.id.checkbox);
        checkbox.setOnClickListener(this);
        qqLogin.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.qqLogin :
                qq = ShareSDK.getPlatform(QQ.NAME);
                //移除授权状态和本地缓存，下次授权会重新授权
                qq.authorize();
                qq.removeAccount(true);
                //SSO授权，传false默认是客户端授权
                qq.SSOSetting(false);
                //授权回调监听，监听oncomplete，onerror，oncancel三种状态
                qq.setPlatformActionListener(platformActionListener);
                //要数据不要功能，主要体现在不会重复出现授权界面
                break;
            case R.id.checkbox :
                MobSDK.submitPolicyGrantResult(checkbox.isChecked(), null);
                qqLogin.setEnabled(checkbox.isChecked());
                break;
        }
    }
    
}